﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-16
Description:SenderList三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class SenderListService
	{
		#region 增加一条数据
		public static int AddSenderList(SenderList senderList)
		{
			string sql =
			"INSERT INTO SenderList(senderID,sendername,tel,address,state,psw,utype,utitle) "+
			"VALUES (@senderID,@sendername,@tel,@address,@state,@psw,@utype,@utitle)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@senderID",senderList.SenderID),
						
					new SqlParameter("@sendername",senderList.Sendername),
						
					new SqlParameter("@tel",senderList.Tel),
						
					new SqlParameter("@address",senderList.Address),
						
					new SqlParameter("@state",senderList.State),
						
					new SqlParameter("@psw",senderList.Psw),
						
					new SqlParameter("@utype",senderList.Utype),
						
					new SqlParameter("@utitle",senderList.Utitle)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSenderListById(long id)	
		{
			string sql="DELETE "+"SenderList"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySenderListByid(SenderList senderList)
		{
		string sql ="UPDATE "+"SenderList"+" set "+
					"senderID=@senderID, "+
					"sendername=@sendername, "+
					"tel=@tel, "+
					"address=@address, "+
					"state=@state, "+
					"psw=@psw, "+
					"utype=@utype, "+
					"utitle=@utitle "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",senderList.Id),
					new SqlParameter("@senderID",senderList.SenderID),
					new SqlParameter("@sendername",senderList.Sendername),
					new SqlParameter("@tel",senderList.Tel),
					new SqlParameter("@address",senderList.Address),
					new SqlParameter("@state",senderList.State),
					new SqlParameter("@psw",senderList.Psw),
					new SqlParameter("@utype",senderList.Utype),
					new SqlParameter("@utitle",senderList.Utitle)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<SenderList> GetAllSenderLists()
		{
			string sqlAll = "SELECT * FROM SenderList";
			return GetSenderListsBySql(sqlAll);
		}
		//以标识列获得对象
		public static SenderList GetSenderListByid(object id)
		{
			string sql = "SELECT * FROM "+"SenderList"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					SenderList senderList = new SenderList();
					senderList.Id=Convert.ToInt32(reader["id"]);
					senderList.SenderID=Convert.ToString(reader["senderID"]);
					senderList.Sendername=Convert.ToString(reader["sendername"]);
					senderList.Tel=Convert.ToString(reader["tel"]);
					senderList.Address=Convert.ToString(reader["address"]);
					senderList.State=Convert.ToString(reader["state"]);
					senderList.Psw=Convert.ToString(reader["psw"]);
					senderList.Utype=Convert.ToString(reader["utype"]);
					senderList.Utitle=Convert.ToString(reader["utitle"]);
					reader.Close();
					
					return senderList;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<SenderList> GetSenderListsBySql(string safeSql)
		{
			List<SenderList> list = new List<SenderList>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					SenderList senderList = new SenderList();
					senderList.Id=Convert.ToInt32(row["id"]);
					senderList.SenderID=Convert.ToString(row["senderID"]);
					senderList.Sendername=Convert.ToString(row["sendername"]);
					senderList.Tel=Convert.ToString(row["tel"]);
					senderList.Address=Convert.ToString(row["address"]);
					senderList.State=Convert.ToString(row["state"]);
					senderList.Psw=Convert.ToString(row["psw"]);
					senderList.Utype=Convert.ToString(row["utype"]);
					senderList.Utitle=Convert.ToString(row["utitle"]);
					list.Add(senderList);
				}
				return list;
			}
             
		}
		private static IList<SenderList> GetSenderListsBySql( string sql, params SqlParameter[] values )
		{
			List<SenderList> list = new List<SenderList>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					SenderList senderList = new SenderList();
					senderList.Id=Convert.ToInt32(row["id"]);
					senderList.SenderID=Convert.ToString(row["senderID"]);
					senderList.Sendername=Convert.ToString(row["sendername"]);
					senderList.Tel=Convert.ToString(row["tel"]);
					senderList.Address=Convert.ToString(row["address"]);
					senderList.State=Convert.ToString(row["state"]);
					senderList.Psw=Convert.ToString(row["psw"]);
					senderList.Utype=Convert.ToString(row["utype"]);
					senderList.Utitle=Convert.ToString(row["utitle"]);
					list.Add(senderList);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<SenderList> ShowSenderListForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from SenderList where  id not in ( select top "
						   +number+" id from SenderList order by id desc ) order by id desc";
			return GetSenderListsBySql(strsql);
		}
		#endregion
	}
}

